/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RMiniImageJDialog.java
 *
 * Created on 2011-7-3, 11:28:16
 */
package seso;

/**
 *
 * @author Administrator
 */
public class RMiniImageJDialog extends javax.swing.JDialog {

    int originalX = 0, originalY = 0, originalWidth = 0, originalHeight = 0, goalX = 0, goalY = 0;
    boolean big2Small = true;

    /** Creates new form RMiniImageJDialog */
    public RMiniImageJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void setImage(java.awt.Image dialogImage, int originalX, int originalY, int originalWidth, int originalHeight, int goalX, int goalY, boolean big2Small) {
        this.originalX = originalX;
        this.originalY = originalY;
        this.originalWidth = originalWidth;
        this.originalHeight = originalHeight;
        this.goalX = goalX;
        this.goalY = goalY;
        this.big2Small = big2Small;

        setSize(originalWidth, originalHeight);
        setPreferredSize(new java.awt.Dimension(originalWidth, originalHeight));
        setBounds(originalX, originalY, originalWidth, originalHeight);

        miniImageJLabel.setFillMethod(0);
        miniImageJLabel.setImage(dialogImage);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        miniImageJLabel = new seso.RImageBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        miniImageJLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(seso.SesoApp.class).getContext().getResourceMap(RMiniImageJDialog.class);
        miniImageJLabel.setText(resourceMap.getString("miniImageJLabel.text")); // NOI18N
        miniImageJLabel.setName("miniImageJLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(miniImageJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(miniImageJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

        float step;
        int from;
        int totalStep = 20;
        int counter;
        int miniWidth = 100, miniHeight = 75;
        int stepWidth, stepHeiht;

        stepWidth = Math.abs(originalWidth - miniWidth) / totalStep;
        stepHeiht = Math.abs(originalHeight - miniHeight) / totalStep;


        if (Math.abs(goalX - originalX) >= Math.abs(goalY - originalY)) {
            step = (float) (goalX - originalX) / totalStep;

            float ratioYX = ((float) (goalY - originalY)) / (goalX - originalX);

            counter = 0;
            while (counter <= totalStep) {
                if (big2Small) {
                    setBounds((int) (counter * step) + originalX, (int) (ratioYX * (counter * step)) + originalY,
                            originalWidth - stepWidth * counter, originalHeight - stepHeiht * counter);
                } else {
                    setBounds((int) (counter * step) + originalX, (int) (ratioYX * (counter * step)) + originalY,
                            miniWidth + stepWidth * counter, miniHeight + stepHeiht * counter);
                }
                try {
                    Thread.sleep(11);
                } catch (Exception ignore) {
                }
                counter++;
            }
        } else {
            step = (goalY - originalY) / totalStep;
            float ratioXY = ((float) (goalX - originalX)) / (goalY - originalY);

            counter = 0;
            while (counter <= totalStep) {
                if (big2Small) {
                    setBounds((int) (ratioXY * (counter * step)) + originalX, (int) (counter * step) + originalY,
                            originalWidth - stepWidth * counter, originalHeight - stepHeiht * counter);
                } else {
                    setBounds((int) (ratioXY * (counter * step)) + originalX, (int) (counter * step) + originalY,
                            miniWidth + stepWidth * counter, miniHeight + stepHeiht * counter);
                }

                try {
                    Thread.sleep(11);
                } catch (Exception ignore) {
                }
                counter++;
            }
        }

        dispose();
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                RMiniImageJDialog dialog = new RMiniImageJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private seso.RImageBox miniImageJLabel;
    // End of variables declaration//GEN-END:variables
}
